package com.yykj.common.handler;

import com.yykj.common.exception.FeignClientException;
import com.yykj.common.util.GsonUtil;
import com.yykj.common.vo.ResultVO;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class ExceptionErrorDecoder  implements ErrorDecoder {
    @Override
    public Exception decode(String methodKey, Response response) {
        try {
            if (response.body() != null) {
                String s = Util.toString(response.body().asReader());
                ResultVO resultVO = GsonUtil.fromJson(s, ResultVO.class);
                if(resultVO!=null){
                    return new FeignClientException(resultVO.getCode(),resultVO.getMsg());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return e;
        }
        return null;
    }
}
